package com.wyq.new_springboot.controller;

import com.wyq.new_springboot.entity.TBlog;
import com.wyq.new_springboot.entity.TUser;
import com.wyq.new_springboot.mapper.TUserMapper;
import com.wyq.new_springboot.service.ITBlogService;
import com.wyq.new_springboot.service.ITUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    ITBlogService itBlogService;
    @Autowired
    ITUserService itUserService;
    @RequestMapping("/article")
    public String toPage(){
        return "publishArticle";
    }
    @RequestMapping("/publish")
    @ResponseBody
    public Map doForm(
            HttpServletRequest request,
            @RequestParam String title,
            @RequestParam String type,
            @RequestParam String content,
            @RequestParam String state
    ){
        Cookie[] cookies = request.getCookies();
        Integer id=0;
        for (Cookie c:cookies) {
            if(c.getName().equals("userName")){
                String name = c.getValue();
                List<TUser> user_name = itUserService.selectByColumn("user_name", name);
                TUser tUser = user_name.get(0);
                id = tUser.getId();
            }
        }
        itBlogService.insertBlog(new TBlog(null,id,title,type,content, LocalDateTime.now(),Integer.valueOf(state)));
        HashMap<String, Object> map = new HashMap<>();
        map.put("state","success");
        return map;
    }
}
